/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewConcertPanel.java
 *
 * Created on Aug 6, 2010, 1:12:59 PM
 */

package org.dafe.partyband.gui.concerts;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.DialogDescriptor;
import org.openide.NotificationLineSupport;
import org.openide.util.NbBundle;

/**
 *
 * @author dafe
 */
public class NewConcertPanel extends javax.swing.JPanel {
    private DialogDescriptor descriptor;

    /** Creates new form NewConcertPanel */
    public NewConcertPanel() {
        initComponents();

        Date curDate = new Date();
        datePicker.setDate(curDate);
        datePicker.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                checkValidity();
            }
        });
        DateFormat ti = SimpleDateFormat.getTimeInstance(DateFormat.SHORT);
        startHourField.setText(ti.format(curDate));

        DocumentListener docL = new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                checkValidity();
            }

            public void removeUpdate(DocumentEvent e) {
                checkValidity();
            }

            public void changedUpdate(DocumentEvent e) {
                checkValidity();
            }
        };

        locationField.getDocument().addDocumentListener(docL);
        startHourField.getDocument().addDocumentListener(docL);
    }

    public Date getConcertDate () {
        return datePicker.getDate();
    }

    public String getConcertLocation () {
        return locationField.getText();
    }

    public String getConcertDescription () {
        return descriptionField.getText();
    }

    public Date getConcertStartHour() {
        DateFormat ti = SimpleDateFormat.getTimeInstance(DateFormat.SHORT);
        Date result = null;
        try {
            result = ti.parse(startHourField.getText());
        } catch (ParseException ex) {
            return null;
        }
        return result;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        datePicker = new org.jdesktop.swingx.JXDatePicker();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        startHourField = new javax.swing.JTextField();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jXLabel4 = new org.jdesktop.swingx.JXLabel();
        locationField = new javax.swing.JTextField();
        jXLabel5 = new org.jdesktop.swingx.JXLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionField = new javax.swing.JTextArea();

        jXLabel1.setText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel1.text", new Object[] {})); // NOI18N
        jXLabel1.setToolTipText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel1.toolTipText", new Object[] {})); // NOI18N

        jXLabel2.setText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel2.text", new Object[] {})); // NOI18N
        jXLabel2.setToolTipText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel2.toolTipText", new Object[] {})); // NOI18N

        startHourField.setText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.startHourField.text", new Object[] {})); // NOI18N
        startHourField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startHourFieldActionPerformed(evt);
            }
        });

        jXLabel3.setText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel3.text", new Object[] {})); // NOI18N

        jXLabel4.setText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel4.text", new Object[] {})); // NOI18N
        jXLabel4.setToolTipText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel4.toolTipText", new Object[] {})); // NOI18N

        locationField.setText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.locationField.text", new Object[] {})); // NOI18N
        locationField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locationFieldActionPerformed(evt);
            }
        });

        jXLabel5.setText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel5.text", new Object[] {})); // NOI18N
        jXLabel5.setToolTipText(org.openide.util.NbBundle.getMessage(NewConcertPanel.class, "NewConcertPanel.jXLabel5.toolTipText", new Object[] {})); // NOI18N

        descriptionField.setColumns(26);
        descriptionField.setRows(3);
        jScrollPane1.setViewportView(descriptionField);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(startHourField, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE))
                    .addComponent(locationField)
                    .addComponent(jScrollPane1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(startHourField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(locationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void startHourFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startHourFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_startHourFieldActionPerformed

    private void locationFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locationFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_locationFieldActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXDatePicker datePicker;
    private javax.swing.JTextArea descriptionField;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXLabel jXLabel4;
    private org.jdesktop.swingx.JXLabel jXLabel5;
    private javax.swing.JTextField locationField;
    private javax.swing.JTextField startHourField;
    // End of variables declaration//GEN-END:variables

    void attachDescriptor(DialogDescriptor descriptor) {
        this.descriptor = descriptor;
        checkValidity();
    }

    private void checkValidity () {
        NotificationLineSupport notificationLine = descriptor.getNotificationLineSupport();
        if (datePicker.getDate() == null) {
            notificationLine.setErrorMessage(NbBundle.getMessage(NewConcertPanel.class, "ERR_DateEmpty"));
            descriptor.setValid(false);
        } else if (locationField.getText().trim().isEmpty()) {
            notificationLine.setErrorMessage(NbBundle.getMessage(NewConcertPanel.class, "ERR_LocationEmpty"));
            descriptor.setValid(false);
        } else if (getConcertStartHour() == null) {
            notificationLine.setErrorMessage(NbBundle.getMessage(NewConcertPanel.class, "ERR_StartHour"));
            descriptor.setValid(false);
        } else {
            notificationLine.clearMessages();
            descriptor.setValid(true);
        }
    }

}
